package com.prince.util;

import java.security.Key;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.codec.binary.Hex;

import com.prince.exception.EncException;


public class PayEncrypt {
	private static String Algorithm = "desede/CBC/PKCS5Padding"; // 定义 加密算法,可用DES,DESede,Blowfish
	// 向量
	private static String iv = "01234567";
	private static String encoding = "utf-8";

	/**
	 * 3DES加密
	 * 
	 * @param plainText
	 *            普通文本
	 * @return
	 * @throws EncException 
	 * @throws Exception
	 */
	public static String encryptMode(String secretKey, String plainText) throws EncException{
		Key deskey = null;
		try {
			DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
			SecretKeyFactory keyfactory = SecretKeyFactory
					.getInstance("desede");
			deskey = keyfactory.generateSecret(spec);
			Cipher cipher = Cipher.getInstance(Algorithm);
			IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
			byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
			return new String(Base64.encode(encryptData)).replaceAll(" ","");
		} catch (Exception e) {
			throw new EncException(1, "PayEncrypt", "encryptMode", "加密错误", null);
		}
	}
	public static String encryptModeHex(String secretKey, String plainText)
			throws EncException {
		Key deskey = null;
		try {
			DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
			SecretKeyFactory keyfactory = SecretKeyFactory
					.getInstance("desede");
			deskey = keyfactory.generateSecret(spec);
			Cipher cipher = Cipher.getInstance(Algorithm);
			IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
			byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
			return new String(Hex.encodeHex(encryptData));//Base64.encode(encryptData);
		} catch (Exception e) {
			throw new EncException(1, "PayEncrypt", "encryptMode", "加密错误", null);
		}
	}

	
	/**
	 * 3DES解密
	 * 
	 * @param encryptText
	 *            加密文本
	 * @return
	 * @throws Exception
	 */
	public static String decryptMode(String secretKey, String encryptText)
			throws EncException {
		Key deskey = null;
		try {
			DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
			SecretKeyFactory keyfactory = SecretKeyFactory
					.getInstance("desede");
			deskey = keyfactory.generateSecret(spec);
			Cipher cipher = Cipher.getInstance(Algorithm);
			IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
			byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));
			System.out.println(new String(decryptData, encoding));
			return new String(decryptData, encoding);
		} catch (Exception e) {
			e.printStackTrace();
			throw new EncException(1, "PayEncrypt", "encryptMode", "解密错误", null);
		}

	}
	
	public static String decryptModeHex(String secretKey, String encryptText)
			throws EncException {
		Key deskey = null;
		try {
			DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
			SecretKeyFactory keyfactory = SecretKeyFactory
					.getInstance("desede");
			deskey = keyfactory.generateSecret(spec);
			Cipher cipher = Cipher.getInstance(Algorithm);
			IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
			byte[] decryptData = cipher.doFinal(Hex.decodeHex(encryptText.toCharArray()));//Base64.decode(encryptText)
			return new String(decryptData, encoding);
		} catch (Exception e) {
			throw new EncException(1, "PayEncrypt", "encryptMode", "解密错误", null);
		}

	}

	public static void main(String args[]) throws EncException {
		//001 登录
		String key2="80312F805252825746DBD08259358F2250";
//        Map<String,Object> retMap = new HashMap<>();
//        retMap.put("reqCode","001");
//        System.out.println("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)));
//        Map<String,Object> retMa = new HashMap<>();
//        retMa.put("linkman","fxh");
//        retMa.put("pwd",1);
//        System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(retMa)));
		//返回结果
//        PayEncrypt.decryptMode(key2,"1SwjEVc5pocZC8K0NltcjYPSg60cjWlYxNh/PgYe9Om3UrGZq1Q8ErApm+2pQijikW4FnvfiCnJrD7zjzJ2nbCA3U0uOOu2ta4Bsbud6oZtjrtNqiupOT6pFANEQytq9zhkARZnJZMomed4jzk1nFY9BKi4h7NbCeIiEqtcogb/8XY3h0VPxlcJeE+Aa2HRhy480V0EYNevXOhJsZ7mk3A5jHob/e47TSe+XIBrybJxZs37e3vFdVNj6vdMjtt6t");
		 //{"empty":false,"params":{"loginKey":"3493c17a1b58f2242297cccfb677465b","loginName":"王培喜","parentId":2,"retCode":"00","retMsg":"成功","type":"staff","userId":9},"reqCode":"001"}
//		System.out.println(PayEncrypt.encryptModeHex(key2,"wpx15311424434")); //loginKey加密 登录名加手机号


		String str = null;
		//商品登录 100
//		Map<String,Object> retMap = new HashMap<>();
//        retMap.put("reqCode","100");
//		Map<String,Object> ret = new HashMap<>();
//        ret.put("loginName","fxh");
//        ret.put("pwd",1);
//        System.out.println("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)));
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
//		 str = "1SwjEVc5pocZC8K0NltcjYPSg60cjWlYc3nK7vuBySdKO34p/km1XJk2IegWWwmoBIaZMJ9+Y7EdcD22wshLOGoYd3cYPGi/Y6K3t9ha8agVKAKoDdAU51CnKueYFU3rxdrHa2My44JQ2goGNTLHtZgEEX7NDuxmZ75gGB15TtZFgY5JKRmxE6aqks3nAk0bxtRIBiqB9xBoQ0YdX7lLuYqi8iBkM1/ofFT6qQ7A5/lCGQyrKe9Q5hiJbMJ4id+vqb2Zbv4mBhmoi25iw8yWd7p9iJcowObdEpJG3PLYSdfPX2Iu8SprVIawlrGCSYTA";
		//解密结果 {"empty":false,"params":{"retCode":"00","retMsg":"成功","user":{"belongType":"admin","created":1585878942000,"id":1,"linkman":"张三","loginFail":0,"loginName":"fxh","pwd":"1","status":"enable","updated":1585878966000}},"reqCode":"100"}


		//修改密码  101
//		Map<String,Object> retMap = new HashMap<>();
//		retMap.put("reqCode","101");
//		Map<String,Object> ret = new HashMap<>();
//        ret.put("oldPwd","1");
//        ret.put("pwd","2");
//        ret.put("newPwd","2");
//        ret.put("loginName","fxh");
//        System.out.println("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)) + "&");
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
//		str = "1SwjEVc5pocZC8K0NltcjYPSg60cjWlYc3nK7vuBySdKO34p/km1XJk2IegWWwmoBIaZMJ9+Y7H7M+L8SLtQiTq/KyuKF7teEbbMBb5wQ64=";
		//解密结果  {"empty":false,"params":{"retCode":"00","retMsg":"成功"},"reqCode":"101"}


		// 查询 商品类型 103
//		Map<String,Object> retMap = new HashMap<>();
//		retMap.put("reqCode","103");
//		Map<String,Object> ret = new HashMap<>();
//		System.out.print("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)) + "&");
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
//		str = "1SwjEVc5pocZC8K0NltcjYPSg60cjWlYfGJw+Ln3i4e+t36LsGB35D0zGstHL3NnPaDs+iTi473ofDKZmOBJ4B+oa1usV1diO8WkdrngE3omxTIcaEg0tUXGAn4I5TrcMcs5s+EpnQ4oUrh4vAVxfmphKL3HMpR+xjrNyMjSJF0r4r0TpUZdpIjlb0hndyFMNIq0VALFZdSoUMQjbuX5VxHeFUjL0pVSgWoRy41MlYqKToxsqi0gF3M5jbQZ1tApZBUo/z9ITlyhJ19k8taOhYlSnabjUAPR2Nyro/PatzPoeTsCZxL0Ddd4p8xhluNU8h4/iw5mI8tN3SoJs0Yei+J8BoDrbMQPdEYFN2ewjeqMsSrJjgNTkdou4EA9XsBpFMj/JLuHjElcR5i5DReimU3FThB41TV0p9PbDAWFpgSF3fFpd3OKVa7aSynZEtqZ1n3ohHgAjDgD5manvzBxzA==";
		//解密结果 {"empty":false,"params":{"data":[{"created":1585880442000,"creater":"张三","id":1,"name":"洗化类","sort":2,"updated":1585880442000,"url":"http://58968.vip/aaa.jpg"},{"created":1585880583000,"creater":"张三","del":"yes","id":3,"name":"母婴类","sort":3,"url":"http://58968.vip/aaa.jpg"}],"retCode":"00","retMsg":"成功"},"reqCode":"103"}


		//添加商品类型  104
//		Map<String,Object> retMap = new HashMap<>();
//		retMap.put("reqCode","104");
//		Map<String,Object> ret = new HashMap<>();
//		ret.put("creater","张三");
//		ret.put("name","母婴类");
//		ret.put("url","http://58968.vip/aaa.jpg");
//		ret.put("sort","3");
//		System.out.print("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)) + "&");
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
//		str = "1SwjEVc5pocZC8K0NltcjYPSg60cjWlYc3nK7vuBySdKO34p/km1XJk2IegWWwmoBIaZMJ9+Y7H7M+L8SLtQiTq/KyuKF7te2HO78WejQm0=";
		//解密结果  {"empty":false,"params":{"retCode":"00","retMsg":"成功"},"reqCode":"104"}


		//修改商品类型  105
//		Map<String,Object> retMap = new HashMap<>();
//		retMap.put("reqCode","105");
//		Map<String,Object> ret = new HashMap<>();
//		ret.put("creater","张三");
//		ret.put("name","洗化类");
//		ret.put("url","http://58968.vip/aaa.jpg");
//		ret.put("sort","2");
//		ret.put("id","1");
//		ret.put("created","张三");
//		System.out.print("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)) + "&");
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
//		str = "1SwjEVc5pocZC8K0NltcjYPSg60cjWlYc3nK7vuBySdKO34p/km1XJk2IegWWwmoBIaZMJ9+Y7H7M+L8SLtQiTq/KyuKF7te62szaGqip3U=";
		//解密结果  {"empty":false,"params":{"retCode":"00","retMsg":"成功"},"reqCode":"105"}


		//查询  搜索 107
//		Map<String,Object> retMap = new HashMap<>();
//		retMap.put("reqCode","107");
//		Map<String,Object> ret = new HashMap<>();
//		ret.put("page","1");
//		// 搜索
//		ret.put("content","222");
//		// name 商品名称  advertising 广告语  described 描述  specifications 规格
//		ret.put("type","name");
//
//		//商品列表点击
//		ret.put("shopId","1");
//		//搜索  商品列表点击 条件二选一
//		System.out.print("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)) + "&");
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
	  //{"empty":false,"params":{"data":[{"advertising":"66666666","created":1585881711000,"del":"yes","described":"555555","id":2,"image":"http://58968.vip/aaa.jpg,http://58968.vip/bbb.jpg","name":"222","settlementMoney":"199.5","shopId":1,"specifications":"dddd","takeOutMoney":"300"}],"retCode":"00","retMsg":"成功"},"reqCode":"107"}


		//添加商品 018
//		Map<String,Object> retMap = new HashMap<>();
//		retMap.put("reqCode","108");
//		Map<String,Object> ret = new HashMap<>();
//		ret.put("image","http://58968.vip/aaa.jpg,http://58968.vip/bbb.jpg");
//		ret.put("shopId","1");
//		ret.put("name","SoLove米菲纸尿裤 （NB码箱装）");
//		ret.put("specifications","NB码36片*4包");
//		ret.put("advertising","1231313454464311313");
//		ret.put("described","22222222222222222222");
//		ret.put("settlementMoney","199.5");
//		ret.put("takeOutMoney","300");
//		System.out.print("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)) + "&");
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
//		str = "1SwjEVc5pocZC8K0NltcjYPSg60cjWlYc3nK7vuBySdKO34p/km1XJk2IegWWwmoBIaZMJ9+Y7H7M+L8SLtQiTq/KyuKF7teaW22mWOTh+s=";
		//{"empty":false,"params":{"retCode":"00","retMsg":"成功"},"reqCode":"108"}


		//修改商品 109
//		Map<String,Object> retMap = new HashMap<>();
//		retMap.put("reqCode","109");
//		Map<String,Object> ret = new HashMap<>();
//		ret.put("image","http://58968.vip/aaa.jpg,http://58968.vip/bbb.jpg");
//		ret.put("shopId","1");
//		ret.put("id","1");
//		ret.put("name","SoLove米菲纸尿裤 （NB码箱装）");
//		ret.put("specifications","NB码36片*4包");
//		ret.put("advertising","22222222");
//		ret.put("described","33333333333333");
//		ret.put("settlementMoney","199.5");
//		ret.put("takeOutMoney","300");
//		System.out.print("keyParam=" + PayEncrypt.encryptMode(key2, JsonUtil.toJSONString(retMap)) + "&");
//		System.out.println("reqJson=" +PayEncrypt.encryptMode(key2,JsonUtil.toJSONString(ret)));
//		str = "1SwjEVc5pocZC8K0NltcjYPSg60cjWlYc3nK7vuBySdKO34p/km1XJk2IegWWwmoBIaZMJ9+Y7H7M+L8SLtQiTq/KyuKF7tefQxPgYf8fME=";
		//{"empty":false,"params":{"retCode":"00","retMsg":"成功"},"reqCode":"109"}
		//解密
//		PayEncrypt.decryptMode(key2,str);
	}

}
